import turtle as tu
import random as ra
import time

tu.setup(1.0,1.0)
tu.bgcolor('black') # 设置画布颜色
tu.title("雪花")
t = tu.Pen()
t.ht() # 隐藏画笔
colors = ['white']

# 雪花类
class SnowFlake():
    def __init__(self):
        self.r = ra.uniform(2,4) # 雪花半径
        self.x = ra.randint(-1000,1000) # 雪花的横坐标
        self.y = ra.randint(500,1000) # 雪花的纵坐标
        self.speed = ra.randint(5,20) # 雪花移动速度
        self.color = ra.choice(colors) # 雪花的颜色
        self.outline = 2 # 雪花的大小

    # 画每个雪花
    def snow(self):
        x = self.r # 雪花的半径
        t.pensize(self.outline) # 雪花的大小
        t.penup() # 提笔
        t.goto(self.x, self.y) # 随机位置
        t.pendown() # 落笔
        t.color(self.color) # 雪花颜色
        for i in range(6):
            t.forward(x * 5)
            t.backward(x * 2)
            t.left(60)
            t.forward(x * 2)
            t.backward(x * 2)
            t.right(120)
            t.forward(x * 2)
            t.backward(x * 2)
            t.left(60)
            t.backward(x * 3)
            t.right(60)

    # 雪花移动函数
    def move(self):
        # 判断雪花是否在画布内
        if self.y >= -500: # 当雪花还在画布中时
            self.y -= self.speed # 设置上下移动速度
        else: # 当雪花不在画布内时，重新生成一个雪花
            self.r = ra.uniform(2,4)
            self.x = ra.randint(-1000,1000)
            self.y = 500
            self.speed = ra.randint(5,20)
            self.color = ra.choice(colors)
            self.outline = 2

Snows = []
for i in range(595):
    Snows.append(SnowFlake())
t.penup()
t.goto(0,0)
t.pendown()
t.pencolor("white")
t.write("张万森", align = "center" , font = ("Comic Sans MS" , 66 , "bold"))
time.sleep(1)
t.clear()
time.sleep(1)
t.write("下雪了", align = "center" , font = ("Comic Sans MS" , 66 , "bold"))
time.sleep(1)
t.clear()
time.sleep(1)

while True:
    tu.tracer(0)
    t.clear()
    t.penup()
    t.goto(0,0)
    t.pendown()
    t.write("我 好 想  你", align = "center" , font = ("Comic Sans MS" , 66 , "bold"))
    for i in range(199):
        Snows[i].move()
        Snows[i].snow()
    tu.update()

tu.mainloop()